<?php
exists_frame();
//调度器
final class app {
    protected $app_instance;
    protected $app_method;
    protected $cfg;
    public function __construct() {
        $this->define_env_constanst();
        $this->load_cfg();
        $this->set_time_zone();
        $this->load_application_class_file();
        //$this->uri_analyze();
    }
    private function define_env_constanst() {
        $in_root = dirname($_SERVER['PHP_SELF']) == '/' ;
        if($in_root) {
            define('WEB_ROOT',dirname($_SERVER['SCRIPT_FILENAME']));
        } else {
            define('WEB_ROOT',str_replace($_SERVER['PHP_SELF'],'',$_SERVER['SCRIPT_FILENAME']));
        }
        define('APP_ROOT',findappdir(WEB_ROOT));
        define('APP_DIRECTORY_NAME', findappname(WEB_ROOT));
        define('CALL_PAGE_NAME',basename($_SERVER['SCRIPT_FILENAME'],'.php'));
        define('CALL_PAGE_DIR',dirname($_SERVER['SCRIPT_FILENAME']));
    }
    public function load_application_class_file() {
        include_once(CALL_PAGE_DIR . '/index.php');
        if(strtolower(CALL_PAGE_NAME) != 'index') {
            $function = CALL_PAGE_NAME;
            include_once($_SERVER['SCRIPT_FILENAME']);
        } else {
            $function = 'defaults';
        }
        $this->app_instance = new index($this->cfg);
        $this->app_instance->run($function);
    }
    public function load_cfg() {
        include_once(FRAMEWORK_ROOT . '/config.default.php');
        if(file_exists(APP_ROOT. '/data/config.php')) {
            include_once(APP_ROOT . '/data/config.php');
        }
        $this->cfg = $cfg;
    }
    public function set_time_zone() {
        if(empty($this->cfg['timezone'])) {
            if(SHOW_DEBUG) throw new debug('Application timezone unset in config file ');
            include_once(FRAMEWORK_ROOT . '/data/config.default.php');
            date_default_timezone_set($cfg['timezone']);
            return;
        }
        date_default_timezone_set($this->cfg['timezone']);
    }
    public function __destruct() {
    }
}
